﻿using System;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace QuanLyKTX.Utilities
{
    public class MdiFormLoader
    {
        private static HybridDictionary m_InitializedForms = new HybridDictionary();

        public static void LoadFormType(Type formType, Form mdiParentForm)
        {
            if (IsAlreadyLoaded(formType))
            {
                return;
            }

            FlagAsLoaded(formType);
            Form frm = (Form)Activator.CreateInstance(formType);            
            frm.MdiParent = mdiParentForm;            
            frm.Closed+=new EventHandler(FormClosed);            
            frm.Show();                        
        }

        private static void FlagAsLoaded(Type formType)
        {
            m_InitializedForms[formType.Name] = true;
        }

        private static void FlagAsNotLoaded(Type formType)
        {
            m_InitializedForms[formType.Name] = false;   
        }

        private static bool IsAlreadyLoaded(Type formType)
        {
            return ((m_InitializedForms[formType.Name]!=null)&&(bool)m_InitializedForms[formType.Name] == true);
        }

        private static void FormClosed(object sender, EventArgs e)
        {
            Form closingForm = (Form)sender;
            closingForm.Closed -= new EventHandler(FormClosed);
            FlagAsNotLoaded(sender.GetType());
        }
       
    }
}
